package com.atguigu.edu.pubshier.controller;

import com.atguigu.edu.pubshier.service.KeyWordSateService;
import com.atguigu.edu.pubshier.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 洛尘
 * @since: 2023-10-22 22:27
 * @description:
 **/
@RestController
public class KeyWordSateServiceController {

    @Autowired
    KeyWordSateService keyWordSateService;

    @RequestMapping("/keyword")
    public String getKeyWord(@RequestParam(value = "date",defaultValue = "0")Integer date){
        if (date==0){
            date= DateFormatUtil.now();
        }
        List<String> keyWords = keyWordSateService.getKeyWords(date);
//        return keyWords;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{\"status\": 0,\"msg\": \"\", \"data\": [");
        for (int i = 0; i < keyWords.size(); i++) {
            String keyWord = keyWords.get(i);
            stringBuilder.append("{\"name\": \""+keyWord+"\",\"value\": 1}");
            if(i < keyWords.size() - 1){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]}");
        return stringBuilder.toString();
    }
}